<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>基础数据管理</el-breadcrumb-item>
      <el-breadcrumb-item>申请信息管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-row :gutter="20">
      <el-col :span="6">
        <el-card>
          <h3>
            <span>剩余资金：{{ money }}</span>
          </h3>
        </el-card>
      </el-col>
      <el-col :span="18">
        <!-- 账单查找 -->
        <el-card class="top-card">
          <el-form :inline="true" :model="search" class="demo-form-inline">
            <el-form-item label="账单ID">
              <el-input
                v-model="search.jobId"
                placeholder="请输入账单ID"
              ></el-input>
            </el-form-item>
            <el-form-item label="日期">
              <div class="block">
                <el-date-picker
                  v-model="search.date"
                  type="daterange"
                  align="right"
                  unlink-panels
                  value-format="yyyy-MM-dd HH:mm:ss"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :picker-options="pickerOptions"
                >
                </el-date-picker>
              </div>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="billSelect">查询</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>

    <!-- 项目岗位树形控件，订单数据表 -->
    <el-row :gutter="20">
      <el-col :span="8">
        <!-- 树形控件 -->
        <el-card class="tree-card">
          <h3>账单筛选</h3>
          <h4>项目-岗位</h4>
          <el-input placeholder="输入关键字进行过滤" v-model="filterText">
          </el-input>
          <el-tree
            class="filter-tree"
            :data="treeData"
            :props="treeProps"
            @node-click="treeNodeClick"
            default-expand-all
            node-key="id"
            :filter-node-method="filterNode"
            ref="tree"
          ></el-tree>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-card class="table-card">
          <!-- 账单数据  资金申请  标签页 -->
          <el-tabs v-model="activeName" type="card" @tab-click="tabsClick">
            <!-- 账单数据表 -->
            <el-tab-pane label="开销账单" name="first">
              <el-table
                height="26rem"
                :data="billData"
                stripe
                style="width: 100%"
              >
                <el-table-column prop="id" label="账单id" width="120">
                </el-table-column>
                <el-table-column prop="time" label="账单生成时间" width="200">
                </el-table-column>
                <el-table-column prop="expense" label="出账额" width="120">
                </el-table-column>
                <el-table-column
                  prop="student.realName"
                  label="出账人"
                  width="120"
                >
                </el-table-column>
                <el-table-column prop="job.name" label="岗位名称">
                </el-table-column>
                <el-table-column prop="adminName" label="记账人" width="120">
                </el-table-column>
                <el-table-column
                  align="center"
                  prop="address"
                  width="100"
                  label="操作"
                >
                  <template slot-scope="scope">
                    <el-button
                      type="text"
                      @click="billDetail(scope.row)"
                      size="small"
                      >详情</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>

            <!-- 资金申请审核表 -->
            <el-tab-pane label="资金申请审核" name="second">
              <el-table
                height="26rem"
                :data="projectData"
                stripe
                style="width: 100%"
              >
                <el-table-column prop="id" label="项目id" width="120">
                </el-table-column>
                <el-table-column prop="name" label="项目名" width="150">
                </el-table-column>
                <el-table-column prop="description" label="项目描述">
                </el-table-column>
                <el-table-column prop="salary" label="项目申请资金" width="150">
                </el-table-column>
                <el-table-column align="center" width="100" label="操作">
                  <template slot-scope="scope">
                    <el-button
                      @click="checkClick(scope.row)"
                      size="small"
                      type="warning"
                    >
                      <i class="el-icon-s-check icon">
                        <span>审 核</span>
                      </i>
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
          </el-tabs>

          <Paging :PageData="page"></Paging>
        </el-card>
      </el-col>
    </el-row>

    <!-- 审核按钮弹窗 -->
    <el-dialog title="项目资金申请审核" :visible.sync="checkDialogVisible">
      <el-descriptions title="项目基本信息" border>
        <el-descriptions-item label="项目id">{{
          checkProjectData.id
        }}</el-descriptions-item>
        <el-descriptions-item label="项目名">{{
          checkProjectData.name
        }}</el-descriptions-item>
        <el-descriptions-item label="项目申请资金">
          <b class="danger">{{ checkProjectData.salary.toFixed(2) }}</b>
        </el-descriptions-item>
        <el-descriptions-item label="项目起止时间">{{
          checkProjectData.startEndTime
        }}</el-descriptions-item>
        <el-descriptions-item label="项目描述">{{
          checkProjectData.description
        }}</el-descriptions-item>
      </el-descriptions>

      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          v-loading.fullscreen.lock="fullscreenLoading"
          @click="applySuccess"
        >
          同意申请
        </el-button>
        <el-button
          type="danger"
          v-loading.fullscreen.lock="fullscreenLoading"
          @click="applyFail"
        >
          拒绝申请
        </el-button>
      </span>
    </el-dialog>

    <!-- 账单详情按钮弹窗 -->
    <el-dialog title="账单详情" :visible.sync="billDetailDialogVisible">
      <el-descriptions :column="2">
        <el-descriptions-item label="账单id">{{
          billDetailData.id
        }}</el-descriptions-item>
        <el-descriptions-item label="记账人id">{{
          billDetailData.adminId
        }}</el-descriptions-item>
        <el-descriptions-item label="结算日期">{{
          billDetailData.time
        }}</el-descriptions-item>
        <el-descriptions-item label="记账人姓名">{{
          billDetailData.adminName
        }}</el-descriptions-item>
        <el-descriptions-item label="出账额">
          <b class="danger">{{ billDetailData.expense.toFixed(2) }}</b>
        </el-descriptions-item>
      </el-descriptions>

      <!-- 分割线 -->
      <el-divider></el-divider>

      <!-- 项目详情 -->
      <el-descriptions title="项目详情" border>
        <el-descriptions-item label="项目id">
          {{ billDetailData.project.id }}
        </el-descriptions-item>
        <el-descriptions-item label="项目名称">
          {{ billDetailData.project.name }}
        </el-descriptions-item>
        <el-descriptions-item label="项目资金配额">
          {{ billDetailData.project.salary }}
        </el-descriptions-item>
        <el-descriptions-item label="项目岗位数量">
          {{ billDetailData.project.jobNum }}
        </el-descriptions-item>
        <el-descriptions-item label="项目起止时间">
          {{ billDetailData.project.startEndTime }}
        </el-descriptions-item>
      </el-descriptions>

      <!-- 分割线 -->
      <el-divider></el-divider>

      <!-- 岗位详情 -->
      <el-descriptions title="岗位详情" border>
        <el-descriptions-item label="岗位id">
          {{ billDetailData.job.id }}
        </el-descriptions-item>
        <el-descriptions-item label="岗位名称">
          {{ billDetailData.job.name }}
        </el-descriptions-item>
        <el-descriptions-item label="岗位薪资">
          {{ billDetailData.job.salary }}
        </el-descriptions-item>
        <el-descriptions-item label="工作地址">
          {{ billDetailData.job.address }}
        </el-descriptions-item>
        <el-descriptions-item label="工作起止日期">
          {{ billDetailData.job.workDate }}
        </el-descriptions-item>
        <el-descriptions-item label="工作时间">
          {{ billDetailData.job.workTime }}
        </el-descriptions-item>
        <el-descriptions-item label="岗位申请时间">
          {{ billDetailData.jobApplyTime }}
        </el-descriptions-item>
      </el-descriptions>

      <!-- 分割线 -->
      <el-divider></el-divider>

      <!-- 学生详情 -->
      <el-descriptions title="学生信息" border>
        <el-descriptions-item label="学号">
          {{ billDetailData.student.id }}
        </el-descriptions-item>
        <el-descriptions-item label="姓名">
          {{ billDetailData.student.realName }}
        </el-descriptions-item>
        <el-descriptions-item label="性别">
          {{ billDetailData.student.sex | setSexText }}
        </el-descriptions-item>
        <el-descriptions-item label="专业">
          {{ billDetailData.student.major }}
        </el-descriptions-item>
        <el-descriptions-item label="班级">
          {{ billDetailData.student.className }}
        </el-descriptions-item>
        <el-descriptions-item label="手机号">
          {{ billDetailData.student.phone }}
        </el-descriptions-item>
        <el-descriptions-item label="邮箱">
          {{ billDetailData.student.email }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import Paging from "../components/Paging.vue";
import { getJobTree } from "../api/fund";
import { getMoney } from "../api/fund";
import { select } from "../api/fund";
import { getBillList } from "../api/fund";
import { getProjectList } from "../api/fund";
import { audit } from "../api/fund";

export default {
  components: {
    Paging,
  },
  data() {
    return {
      money: 0,
      page: {
        pageSizes: [5, 10, 50, 100],
        pageSize: 5,
        currentPage: 1,
        total: 0,
      },
      page1: {
        pageSizes: [5, 10, 50, 100],
        pageSize: 5,
        currentPage: 1,
        total: 0,
      },
      page2: {
        pageSizes: [5, 10, 50, 100],
        pageSize: 5,
        currentPage: 1,
        total: 0,
      },
      search: {
        jobId: "",
        date: [],
      },
      filterText: "",
      activeName: "first",
      treeProps: {
        children: "jobs",
        label: "name",
      },
      treeData: [],
      projectData: [],
      checkProjectData: {
        id: 0,
        name: '',
        salary: 0,
        startEndTime: '',
        description: '',
      },
      billDetailData: {
        id: 0,
        adminId: 0,
        adminName: "",
        time: "",
        expense: 0,
        jobApplyTime: "",
        project: {},
        job: {},
        student: {},
      },
      billData: [],
      checkDialogVisible: false,
      billDetailDialogVisible: false,
      fullscreenLoading: false,
    };
  },
  methods: {
    billSelect() {
      select({
        id: this.search.jobId,
        startDate: this.search.date[0],
        endDate: this.search.date[1],
        pageSize: this.page.pageSize,
        currentPage: this.page.currentPage,
      }).then((res) => {
        this.activeName = "first";
        this.billData = res.data.data.data;
        this.page1.total = res.data.data.total;
        this.page1.currentPage = res.data.data.currentPage;
        this.page = this.page1;
      });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    checkClick(row) {
      this.checkDialogVisible = true;
      this.checkProjectData = row;
    },
    billDetail(row) {
      this.billDetailDialogVisible = true;
      this.billDetailData = row;
    },
    tabsClick() {
      if (this.activeName === "first") {
        this.page = this.page1;
      } else if (this.activeName === "second") {
        this.page = this.page2;
      }
    },
    treeNodeClick(data) {
      if (data.isProject === true) {
        console.log(data);
      } else {
        select({
          id: data.id,
          pageSize: this.page.pageSize,
          currentPage: this.page.currentPage,
        }).then((res) => {
          this.activeName = "first";
          this.billData = res.data.data.data;
          this.page1.total = res.data.data.total;
          this.page1.currentPage = res.data.data.currentPage;
          this.page = this.page1;
        });
      }
    },
    openFullScreen() {
      this.fullscreenLoading = true;
    },
    pickerOptions() {},
    applySuccess() {
      this.openFullScreen;
      audit({
        id: this.checkProjectData.id,
        status: 1,
      }).then((res) => {
        getProjectList({
          pageSize: this.page.pageSize,
          currentPage: this.page.currentPage,
        }).then((res) => {
          this.projectData = res.data.data.data;
          this.page2.total = res.data.data.total;
          this.page2.currentPage = res.data.data.currentPage;
        });
        this.fullscreenLoading = false;
        this.$message({
          message: res.data.message,
          type: "success",
        });
        this.checkDialogVisible = false;
      });
    },
    applyFail() {
      this.openFullScreen;
      audit({
        id: this.checkProjectData.id,
        status: 2,
      }).then((res) => {
        getProjectList({
          pageSize: this.page.pageSize,
          currentPage: this.page.currentPage,
        }).then((res) => {
          this.projectData = res.data.data.data;
          this.page2.total = res.data.data.total;
          this.page2.currentPage = res.data.data.currentPage;
        });
        this.fullscreenLoading = false;
        this.$message({
          message: res.data.message,
          type: "success",
        });
        this.checkDialogVisible = false;
      });
    },
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  filters: {
    //性别过滤器 根据值 1/0 转换成 男/女
    setSexText(value) {
      if (value == 1) {
        return "男";
      } else if (value == 0) {
        return "女";
      } else {
        return "未知";
      }
    },
  },
  created() {
    getJobTree().then((res) => {
      this.treeData = res.data.data.jobTree;
    });

    getMoney().then((res) => {
      this.money = res.data.data.money;
    });

    getBillList({
      pageSize: this.page.pageSize,
      currentPage: this.page.currentPage,
    }).then((res) => {
      this.billData = res.data.data.data;
      this.page1.total = res.data.data.total;
      this.page1.currentPage = res.data.data.currentPage;
      this.page = this.page1;
    });

    getProjectList({
      pageSize: this.page.pageSize,
      currentPage: this.page.currentPage,
    }).then((res) => {
      this.projectData = res.data.data.data;
      this.page2.total = res.data.data.total;
      this.page2.currentPage = res.data.data.currentPage;
    });
  },
};
</script>

<style lang="less" scoped>
.top-card {
  width: 100%;
}

.tree-card {
  width: 100%;
  height: 35rem;

  .el-input {
    margin-bottom: 0.5rem;
  }
}

.table-card {
  width: 100%;
  height: 35rem;
}
</style>